//
//  SearchTopView.swift
//  GreenDeal
//
//  Created by Moennig Q on 15/12/2022.
//

import UIKit
import SnapKit
import RxSwift
import RxCocoa
class SearchTopView: View {
    fileprivate lazy var disposeBag = DisposeBag()
    var editingChangedClosure: (()->Void)?
    let searchTextfield = BaseTextField()
    let cancelLabel = ContentLabel(R.font.interMedium(size: 15),text: R.string.localizable.cancel(), hexStr: "#FFFFFFFF")
    //
    convenience init(bgImageName: String) {
        self.init(frame: CGRect.zero)
        self.setUpView(bgImageName)
    }
    private func setUpView(_ bgImageName: String) {
        self.backgroundColor = UIColor(hexStr: Constant.SystemBackColor)
        let backImageView = UIImageView(image: UIImage(named: bgImageName))
        backImageView.contentMode = .scaleToFill
        self.addSubview(backImageView)
        backImageView.snp.makeConstraints { make in
            make.left.top.right.equalToSuperview()
            make.height.equalTo(100)
            make.bottom.equalToSuperview()
        }
        //1.3 search back view
        let searchBakView = UIView()
        searchBakView.backgroundColor = UIColor.white.withAlphaComponent(0.95)
        searchBakView.layer.cornerRadius = Constant.BackViewCornerRadius
        searchBakView.layer.borderWidth = 1
        searchBakView.layer.borderColor = UIColor(hexStr: "#292D3233").cgColor
        self.addSubview(searchBakView)
        searchBakView.snp.makeConstraints { (make) in
            make.bottom.equalTo(backImageView).offset(-10)
            make.left.equalTo(16)
            make.width.greaterThanOrEqualTo(0)
            make.height.equalTo(44)
            
        }
        //cancel btn
        cancelLabel.setContentHuggingPriority(.required, for: NSLayoutConstraint.Axis.horizontal)
        cancelLabel.setContentCompressionResistancePriority(.required, for: NSLayoutConstraint.Axis.horizontal)
        self.addSubview(cancelLabel)
        cancelLabel.snp.makeConstraints { make in
            make.left.equalTo(searchBakView.snp.right).offset(10)
            make.centerY.equalTo(searchBakView)
            make.right.equalTo(-16)
            make.height.width.greaterThanOrEqualTo(0)
        }
        //left image view
        let leftImageView = UIImageView(image: R.image.jobs_search_magnifier())
        searchBakView.addSubview(leftImageView)
        leftImageView.snp.makeConstraints { make in
            make.left.equalTo(8)
            make.width.height.equalTo(24)
            make.centerY.equalToSuperview()
        }
        //1.3.1 down arrow icon
        let clearImageView = UIImageView(image: R.image.jobs_search_clear())
        searchBakView.addSubview(clearImageView)
        clearImageView.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-8)
            make.width.height.equalTo(24)
            make.centerY.equalToSuperview()
        }
        //1.3.2 status label
        searchTextfield.enabledAutoAdjustFontSize()
        searchTextfield.setFont(R.font.interMedium(size: 15))
        searchBakView.addSubview(searchTextfield)
        searchTextfield.snp.makeConstraints { (make) in
            make.left.equalTo(leftImageView.snp.right).offset(8)
            make.top.bottom.equalToSuperview()
            make.right.equalTo(clearImageView.snp.left).offset(-8)
        }
        searchTextfield.placeholder = R.string.localizable.search()
        //
        clearImageView.rx.tapGesture().when(.recognized).subscribe { [weak self] _ in
            self?.searchTextfield.text = ""
        }.disposed(by: self.disposeBag)
        //编辑过程中进行查找
        searchTextfield.rx.controlEvent(.editingChanged).subscribe { [weak self] _ in
            guard let strongSelf = self else {return }
            strongSelf.editingChangedClosure?()
        }.disposed(by: self.disposeBag)
    }
}
